package com.itheima.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.itheima.common.utils.UtilFuns;
import com.itheima.domain.system.Module;
import com.itheima.service.system.IModuleService;
import com.itheima.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/system/module")
public class ModelController extends BaseController {

    @Autowired
    private IModuleService moduleService;

    @RequestMapping(value = "/list",name = "分页查询当前页模块数据")
    public String List(@RequestParam(defaultValue = "1") int page) {

        int size = 5;

        PageInfo pageInfo = moduleService.findAll(page, size);

        request.setAttribute("page", pageInfo);

        return "system/module/module-list";
    }

    @RequestMapping(value = "/toAdd",name = "去到模块新建页面")
    public String toAdd() {

        List<Module> moduleList = moduleService.findAll();

        request.setAttribute("menus", moduleList);

        return "system/module/module-add";
    }

    @RequestMapping(value = "/toUpdate",name = "去到模块更新页面")
    public String upDate(String id) {

        Module module = moduleService.findById(id);

        request.setAttribute("module", module);

        List<Module> moduleList = moduleService.findAll();

        request.setAttribute("menus", moduleList);

        return "system/module/module-update";
    }

    @RequestMapping(value = "/edit",name = "保存或者更新模块")
    public String edit(Module module) {
        //判断保存或者更新
        if (UtilFuns.isEmpty(module.getId())) {
            //保存
            moduleService.save(module);
        } else {
            //更新
            moduleService.update(module);
        }

        return "redirect:/system/module/list.do";
    }

    @RequestMapping(value = "/delete",name = "删除模块")
    public String delete(String id) {
        moduleService.delete(id);
        return "redirect:/system/module/list.do";
    }

}
